"""
    @Author  ：思念 
    @File    ：6.jsonpath简单使用.py
    @Date    ：2024/12/5 20:44 
"""

import jsonpath

info = {
    "error_code": 0,
    "stu_info": [
        {
            "id": 2059,
            "name": "小白",
            "sex": "男",
            "age": 28,
            "addr": "河南省济源市北海大道xx号",
            "grade": "天蝎座",
            "phone": "1837830xxxx",
            "gold": 10896,
            "info": {
                "card": 12345678,
                "bank_name": '中国银行'
            }
        },
        {
            "id": 2067,
            "name": "小黑",
            "sex": "男",
            "age": 28,
            "addr": "河南省济源市北海大道xx号",
            "grade": "天蝎座",
            "phone": "87654321",
            "gold": 100
        }
    ]
}
# 不使用 jsonpath
res = info['stu_info'][0]['name']
print(res)

# 使用 jsonpath
res_name = jsonpath.jsonpath(info, '$..name')
print(res_name)
res_bank_name = jsonpath.jsonpath(info, '$..info.bank_name')
print(res_bank_name)

res_stu_info = jsonpath.jsonpath(info, '$..stu_info')
print(res_stu_info)

res_phone = jsonpath.jsonpath(info, '$..phone')[1]
print(res_phone)